package com.mzy.designMode.templateMode;


/**
 * Created with IntelliJ IDEA.
 *
 * @Author mzy
 * @Date 2022/5/31 9:15
 * @Version 1.0
 */

/**
 * 模板方法，定义模板方法
 */
public class TemplateMethodPattern {
    public static void main(String[] args) {
        CookingTomato cookingTomato = new CookingTomato();
        cookingTomato.cook();
        new CookingPotato().cook();
    }
}

// 做饭的功能例子
abstract class Cooking {
    // 步骤
    protected abstract void step1();

    protected abstract void step2();

    // 做饭,template
    public void cook() {
        System.out.println("做饭开始");
        step1();
        step2();
        System.out.println("做饭结束");
    }
}

// 实现子类，炒西红柿
class CookingTomato extends Cooking {

    @Override
    protected void step1() {
        System.out.println("放入鸡蛋和西红柿");
    }

    @Override
    protected void step2() {
        System.out.println("少放盐和多放味精");

    }
}


// 实现子类，炒土豆
class CookingPotato extends Cooking {

    @Override
    protected void step1() {
        System.out.println("放入鸡蛋和土豆");
    }

    @Override
    protected void step2() {
        System.out.println("多放盐和多放味精");

    }
}
